package com.igd.business.service.impl;

import java.sql.SQLException;
import java.util.List;

import org.apache.commons.lang3.StringUtils;

import com.igd.business.dao.IUserDao;
import com.igd.business.service.IUserService;
import com.igd.common.constant.IConstant;
import com.igd.exceptions.IgdException;
import com.igd.pojo.vo.PageList;
import com.igd.security.Resources;
import com.igd.security.Roles;
import com.igd.security.User;
import com.igd.utils.DateUtil;

/**
 * @version 1.0
 * 
 * @author QIAOYU
 * 
 * @date 2013-6-24
 * 
 * @description 用户信息服务类接口实现类
 */

public class IUserServiceImpl implements IUserService {

	private IUserDao userDao;

	public void updateUserInfo(User user) throws SQLException, IgdException {
		
		this.getUserDao().updUser(user);

	}

	public void delUserInfo(User user) throws SQLException, IgdException {
		
		this.getUserDao().deleteUserInfo(user);

	}
	
	public User saveUserInfo(User user) throws SQLException, IgdException {
		
		if(null != user){
			
			user.setUserStatus(IConstant.USER_NORMAL_STATUS);
			
			String currentTime = DateUtil.getLongDate();
			
			user.setUserCreateTime(currentTime);
			
			user.setUserLastLoginTime(currentTime);
			
			this.userDao.insertUserInfo(user);
			
			user.setUserPwd(IConstant.EMPTY_STRING);
			
			return user;
			
		}
		
		return null;
	}
	
	public User getUserInfoByUserName(String loginName) throws SQLException,IgdException {

		if (StringUtils.isNotEmpty(loginName)) {

			User user = new User();

			user.setUserName(loginName);

			return this.getUserDao().queryUser(user);

		}

		return null;
	}

	public User getUserInfoByUserId(String userId) throws SQLException,IgdException {
		
		if(StringUtils.isNotEmpty(userId)){
			
			User user = new User();
			
			user.setUserId(userId);
			
			return this.getUserDao().queryUser(user);

		}
		
		return null;
	}

	public List<Roles> getUserRoleByUserId(String userId) throws SQLException,IgdException {

		if (StringUtils.isNotEmpty(userId)) {

			User user = new User();

			user.setUserId(userId);

			return this.getUserDao().queryUserRole(user);

		}

		return null;
	}

	public List<Resources> getUserRoleResourceByUserId(String userId) throws SQLException, IgdException {

		if (StringUtils.isNotEmpty(userId)) {

			User user = new User();

			user.setUserId(userId);

			return this.getUserDao().queryRoleResource(user);

		}

		return null;
	}

	public List<User> getAllUser(PageList pageList) throws SQLException, IgdException {
		
		if(null != pageList){
			
			return this.getUserDao().queryAllUser(pageList,new User());

		}
		
		return null;
	}
	
	public IUserDao getUserDao() {
		return userDao;
	}

	public void setUserDao(IUserDao userDao) {
		this.userDao = userDao;
	}
}
